<?php
/**
 * Setting controller for JA News Component
 * @package Joomlart
 * @subpackage Component
 */
//Check to ensure this file is included in Joomla
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class JanewsControllercommon extends JController
{
  function display()
  {
    $view = JRequest::getCmd('view');
		if(empty($view)) {
			JRequest::setVar('view', 'common');
			JRequest::setVar('layout', 'config');
		}
    
    parent::display();
  }
  
  function save()
  {
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$post = JRequest::get('post');
		$db		= $this->getModel('common');
		$enable = $post['enabled'];
		$setmenu = ($post['setmenu']==1) ? true : false;
		$sethomepage = ($post['homepage']==1) ? true : false;
		$component =& JComponentHelper::getComponent('com_janews');
		$checkComHomePage = $db->checkComHomePage($component->id);
    $msg = '';

		if ($enable==1 && $setmenu){
		  if ($post['menu']==1){
		    $menu = $db->createComponentMenu($sethomepage);
		    $msg = ($menu=='success') ? 
		      JText::_('CREATED MENU') : JText::_('CREATED MENU');
		      if ($sethomepage) $msg .= ", ". JText::_('DEFAULT HOMPAGE');
		      else $msg .= ", ". JText::_('UNSET DEFAULT HOMPAGE');
		  }else {
		    $msg = JText::_('NOT CREATED MENU');
		  }
	  }
	  //if choose set default homepage=yes and enable component and checkComHomePage = false then set default homepage
	  //if choose set default homepage=no and enable component and checkComHomePage = true then unset default homepage
	   
	  elseif ($enable==1 && ($sethomepage != $checkComHomePage))
	  {
	    if ($sethomepage){
	      $db->setDefaultHomePage($component->id);
	      $msg = JText::_('DEFAULT HOMPAGE');
	    }
	    else {
	      $db->unDefautlHomePage($component->id);
	      $msg = JText::_('UNSET DEFAULT HOMPAGE');
	    }
	  }

		$update = $db->updateComponentTable($enable);
    if ($update){
      $link = 'index.php?option=com_janews';
      $msg .= ($enable==1) ? JText::_('ENABLE COMPONENT') : JText::_('DISABLE COMPONENT');
    }
    else {
    	$link = 'index.php?option=com_janews';
    }
		$this->setRedirect($link,$msg);
  }
  
  
  
}